package org.unit.testmgmt.ui.generator.wizard;

import org.eclipse.jdt.ui.JavaElementLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableColumn;
import org.unit.testmgmt.generator.wizard.GeneratorWizardTestCases;
import org.unit.testmgmt.messages.Messages;
import org.unit.testmgmt.tml.UTMModel;
import org.unit.testmgmt.ui.table.TableViewerBase;

/**
 * The page for the test cases and assertions.
 * 
 * @author Robert Streng
 *
 */
public class GeneratorWizardTestCasesPage extends GeneratorWizardBasePage {

	private ComboViewer cboMethod;

	private Group grpTblTestcases;

	private Composite cmpMainContainer;

	private Group grpTblAssertions;

	private GeneratorWizardTestBasesPage testbasesPage;

	private GeneratorWizardMainPage mainPage;

	public GeneratorWizardTestCasesPage(String title, String description, String pageName,
			UTMModel model, GeneratorWizardMainPage mainPage,
			GeneratorWizardTestBasesPage testbasesPage) {
		super(title, description, pageName, model);
		this.mainPage = mainPage;
		this.testbasesPage = testbasesPage;
	}

	@Override
	public void createControl(Composite parent) {
		cmpMainContainer = new Composite(parent, SWT.FILL);
		cmpMainContainer.setLayout(new GridLayout(2, false));
		setControl(cmpMainContainer);

		// methods
		Label lblMethod = new Label(cmpMainContainer, SWT.NONE);
		lblMethod.setText(Messages.GeneratorWizardTestCasesPage_Method);

		cboMethod = new ComboViewer(cmpMainContainer, SWT.NONE);
		cboMethod.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		JavaElementLabelProvider labelProvider = new JavaElementLabelProvider(
				JavaElementLabelProvider.SHOW_DEFAULT | JavaElementLabelProvider.SHOW_RETURN_TYPE
						| JavaElementLabelProvider.SHOW_TYPE);
		cboMethod.setLabelProvider(labelProvider);

		// test-cases
		grpTblTestcases = new Group(cmpMainContainer, SWT.NONE);
		grpTblTestcases.setText(Messages.GeneratorWizardTestCasesPage_testcases);

		GridData layoutData = new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1);
		layoutData.minimumHeight = 150;
		grpTblTestcases.setLayoutData(layoutData);
		GridLayout gl_cmpTblTestcases = new GridLayout(1, false);
		gl_cmpTblTestcases.marginWidth = 0;
		grpTblTestcases.setLayout(gl_cmpTblTestcases);

		// test-assertions
		grpTblAssertions = new Group(cmpMainContainer, SWT.NONE);
		grpTblAssertions.setText(Messages.GeneratorWizardTestCasesPage_assertions);
		grpTblAssertions.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
		grpTblAssertions.setLayout(new GridLayout(1, false));
	}

	@Override
	protected void createController(UTMModel model) {
		this.controller = new GeneratorWizardTestCases(model, this);
	}

	/**
	 * Layout the table assertions.
	 * 
	 * @param tblAssertions
	 */
	public void layoutTblAssertions(TableViewerBase tblAssertions) {
		for (TableColumn column : tblAssertions.getTable().getColumns()) {
			column.pack();
			if (column.getWidth() > 150) {
				column.setWidth(150);
			}
			else if (column.getWidth() < 50) {
				column.setWidth(50);
			}
		}

		GridData gd = (GridData) tblAssertions.getControl().getLayoutData();
		gd.minimumHeight = 150;
		gd.horizontalIndent = 0;

		grpTblAssertions.layout();
		cmpMainContainer.layout();

	}

	public ComboViewer getCboMethod() {
		return cboMethod;
	}

	public Group getGrpTblTestcases() {
		return grpTblTestcases;
	}

	public Group getGrpTblAssertions() {
		return grpTblAssertions;
	}

	/**
	 * Layout the table test cases.
	 * 
	 * @param tblTestcases
	 */
	public void layoutTblTestcases(TableViewerBase tblTestcases) {
		for (TableColumn column : tblTestcases.getTable().getColumns()) {
			column.pack();
			if (column.getWidth() > 150) {
				column.setWidth(150);
			}
			else if (column.getWidth() < 50) {
				column.setWidth(50);
			}
		}

		GridData gd = (GridData) tblTestcases.getControl().getLayoutData();
		gd.minimumHeight = 150;
		gd.horizontalIndent = 0;

		grpTblTestcases.layout();
		cmpMainContainer.layout();
	}

	public GeneratorWizardTestBasesPage getTestbasesPage() {
		return testbasesPage;
	}

	public GeneratorWizardMainPage getMainPage() {
		return mainPage;
	}

}
